/**
 * 
 */
package org.vsg.stock.core.business;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.vsg.stock.core.utils.StockDateUtilExtension;

/**
 * @author Bill Vison
 *
 */

public class StoppingTradeDaysBO implements StockDateUtilExtension{
	
	private static SimpleDateFormat sdf = new SimpleDateFormat("MM-dd");

	private static final Logger logger = LoggerFactory.getLogger(StoppingTradeDaysBO.class);

	

	/**
	 * set the date
	 * @param date
	 * @return
	 */
	public  boolean isTradingDate(Date date) {
		Date tmpDate = null;
		Calendar tmpCale = Calendar.getInstance();
		tmpCale.setTime(date);		
	
		return isTradingDate(tmpCale);
	}

	public boolean isTradingDate(Calendar tmpCale) {
		
		if (tmpCale.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY 
				|| tmpCale.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY ) {
			return false;
		}

		return true;
	}

	@Override
	public int getDifferenceValue(String stkCode, Date relativeDate,
			Date targetDate) {
		
		Calendar tmpCale = Calendar.getInstance();
		tmpCale.setTime( relativeDate );
		
		// -- after date ---
		int index = 0;
		if (targetDate.after( relativeDate ) ) {
			
			while (tmpCale.getTime().before( targetDate ) ) {

				if (isTradingDate(tmpCale) ) {
					index++;
				}
				tmpCale.add( Calendar.DATE , 1);				
			}
		}
		else if (targetDate.before( relativeDate ) ) {
			while (tmpCale.getTime().after( targetDate ) ) {

				if (isTradingDate(tmpCale) ) {
					index--;
				}
				tmpCale.add( Calendar.DATE , -1);				
			}			
		}

		return index;
	}	

	
	
}
